package com.ruoyi.common.utils;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class ThreadPool {
    private final int poolSize;
    private final List<WorkerThread> workers;
    private final BlockingQueue<Runnable> taskQueue;

    public ThreadPool(int poolSize) {
        this.poolSize = poolSize;
        taskQueue = new LinkedBlockingQueue<>();
        workers = new ArrayList<>();

        for (int i = 0; i < poolSize; i++) {
            WorkerThread worker = new WorkerThread();
            worker.start();
            workers.add(worker);
        }
    }

    public void execute(Runnable task) {
        taskQueue.offer(task);
    }

    private class WorkerThread extends Thread {
        public void run() {
            while (true) {
                try {
                    Runnable task = taskQueue.take();
                    task.run();
                } catch (InterruptedException e) {
                    // Handle interruption
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
}
